﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.Data.SqlClient;

namespace MonitorServer
{
    [DataContract]
    public class WordMonitor
    {
        [DataMember]
        public int WordMonitorID { get; set; }
        [DataMember]
        public string FileName { get; set; }
        [DataMember]
        public string Location { get; set; }
        [DataMember]
        public double LoadAppTime { get; set; }
        [DataMember]
        public double LoadTemplateTime { get; set; }
        [DataMember]
        public double WriteTime { get; set; }
        [DataMember]
        public double SaveTime { get; set; }
        [DataMember]
        public double CloseTime { get; set; }
        [DataMember]
        public DateTime StartTime { get; set; }
        [DataMember]
        public DateTime EndTime { get; set; }

        public DateTime StartTimeFormat
        {
            get
            {
                return new DateTime(StartTime.Year, StartTime.Month, StartTime.Day
                     , StartTime.Hour, (StartTime.Minute / 5) * 5, 0);
            }
        }

        protected string InsertSql
        {
            get
            {
                return string.Format(@"INSERT INTO [WordMonitor]
                                   ([FileName]
                                   ,[Location]
                                   ,[LoadAppTime]
                                   ,[LoadTemplateTime]
                                   ,[WriteTime]
                                   ,[SaveTime]
                                   ,[CloseTime]
                                   ,[StartTime]
                                   ,[EndTime]
                                   ,[StartTimeFormat])
                                   VALUES(@FileName, @Location, @LoadAppTime, @LoadTemplateTime,@WriteTime,@SaveTime
                                    ,@CloseTime,@StartTime,@EndTime,@StartTimeFormat)");
            }
        }

        public void Insert()
        {
            SqlParameter[] sqlParameters = {
                                               new SqlParameter("@FileName", FileName),
                                               new SqlParameter("@Location", Location),
                                               new SqlParameter("@LoadAppTime", LoadAppTime),
                                               new SqlParameter("@LoadTemplateTime", LoadTemplateTime),
                                               new SqlParameter("@WriteTime", WriteTime),
                                               new SqlParameter("@SaveTime", SaveTime),
                                               new SqlParameter("@CloseTime", CloseTime),
                                               new SqlParameter("@StartTime", StartTime),
                                               new SqlParameter("@EndTime", EndTime),
                                               new SqlParameter("@StartTimeFormat", StartTimeFormat),
                                           };

            Utility.SqlHelper.ExecuteNonQuery(InsertSql, sqlParameters);
        }
    }
}
